//=============================================================================
//
//		Copyright (C) 1995 by Paul S. McCarthy and Eric Sunshine.
//				Written by Paul S. McCarthy and Eric Sunshine.
//							All Rights Reserved.
//
//		This notice may not be removed from this source code.
//
//		This object is included in the MiscKit by permission from the authors
//		and its use is governed by the MiscKit license, found in the file
//		"License.rtf" in the MiscKit distribution.	Please refer to that file
//		for a list of all applicable permissions and restrictions.
//		
//=============================================================================
//-----------------------------------------------------------------------------
// MiscTableFocus.M
//
//		Blinking "cursor" for showing keyboard focus in TableView.
//
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// $Id: MiscTableFocus.M,v 1.1 95/09/27 12:21:21 zarnuk Exp $
// $Log:		MiscTableFocus.M,v $
//	Revision 1.1  95/09/27	12:21:21  zarnuk
//	Initial revision
//	
//-----------------------------------------------------------------------------

#import "MiscTableFocus.h"

extern "Objective-C" {
#import <appkit/Application.h>
#import <appkit/View.h>
}
extern "C" {
#import "MiscTableFocusPS.h"
}

double const BLINK = 0.4;		// seconds
enum { FOCUS_BLACK = NO, FOCUS_WHITE = YES };


@implementation MiscTableFocus

//-----------------------------------------------------------------------------
// initOwner:
//-----------------------------------------------------------------------------
- initOwner: (View*) o
	{
	[super init];
	owner = o;
	[owner getBounds: &frame];
	state = FOCUS_BLACK;
	return self;
	}


//-----------------------------------------------------------------------------
// free
//-----------------------------------------------------------------------------
- free
	{
	if (timer != 0)
		DPSRemoveTimedEntry( timer );
	return [super free];
	}


//-----------------------------------------------------------------------------
// canDraw
//-----------------------------------------------------------------------------
- (BOOL) canDraw
	{
	return ([self isShowingFocus] && frame.size.width > 0 &&
				frame.size.height > 0 && [owner canDraw]);
	}


//-----------------------------------------------------------------------------
// drawFocus
//-----------------------------------------------------------------------------
- (void) drawFocus
	{
	if ([self canDraw])
		{
		BOOL needFocus = (![owner isFocusView]);
		if (needFocus) [owner lockFocus];
		MISC_TF_drawBox( frame.origin.x, frame.origin.y,
						 frame.size.width, frame.size.height,
						 state ? NX_WHITE : NX_BLACK );
		if (needFocus) [owner unlockFocus];
		}
	}


//-----------------------------------------------------------------------------
// eraseFocus
//-----------------------------------------------------------------------------
- (void) eraseFocus
	{
	if ([self canDraw])
		{
		BOOL needFocus = (![owner isFocusView]);
		if (needFocus) [owner lockFocus];
		MISC_TF_eraseBox( frame.origin.x, frame.origin.y,
						  frame.size.width, frame.size.height );
		if (needFocus) [owner unlockFocus];
		}
	}


//-----------------------------------------------------------------------------
// setFrame:
//-----------------------------------------------------------------------------
- setFrame: (NXRect const*) r
	{
	[self eraseFocus];
	frame = *r;
	[self drawFocus];
	return self;
	}


//-----------------------------------------------------------------------------
// blinker
//-----------------------------------------------------------------------------
- (void) blinker
	{
	state = !state;
	[self drawFocus];
	}

static void blinker( DPSTimedEntry te, double now, void* userData )
	{
	[id(userData) blinker];
	}


//-----------------------------------------------------------------------------
// startFocus
//-----------------------------------------------------------------------------
- (void) startFocus
	{
	if (![self isShowingFocus])
		{
		timer = DPSAddTimedEntry( BLINK, blinker, self, NX_RUNMODALTHRESHOLD );
		state = FOCUS_BLACK;
		[self drawFocus];
		}
	}


//-----------------------------------------------------------------------------
// stopFocus
//-----------------------------------------------------------------------------
- (void) stopFocus
	{
	if ([self isShowingFocus])
		{
		[self eraseFocus];
		DPSRemoveTimedEntry( timer );
		timer = 0;
		}
	}


//-----------------------------------------------------------------------------
// isShowingFocus
//-----------------------------------------------------------------------------
- (BOOL) isShowingFocus
	{
	return (timer != 0);
	}

@end
